package cn.com.bsoft.mobile.ui

import cn.com.bsoft.mobile.model.AppVersionRsp
import cn.com.bsoft.mobile.service.AppVersionService
import cn.com.bsoft.mobile.service.MobileAppService
import cn.net.xyan.blossom.core.service.ConfigService
import cn.net.xyan.blossom.webUI.utils.getBean
import com.github.vok.karibudsl.*
import com.vaadin.data.provider.ListDataProvider
import com.vaadin.shared.ui.MarginInfo
import com.vaadin.ui.Alignment
import com.vaadin.ui.VerticalLayout
import com.vaadin.ui.renderers.ButtonRenderer
import java.util.*

class VersionManager : VerticalLayout() {

    var versions = LinkedList<AppVersionRsp>()

    init {
        val appService = getBean(MobileAppService::class.java)!!
        val versionService = getBean(AppVersionService::class.java)!!
        val configService = getBean(ConfigService::class.java)!!

        val dataProvider = ListDataProvider(versions)

        isSpacing = false
        margin = MarginInfo(false, false, false, false)
        setSizeFull()
        horizontalLayout {
            //alignment = Alignment.BOTTOM_LEFT
            val cbApp = comboBox<String>(caption = "应用") {

                val map = HashMap<String, String?>()

                appService.allApp().forEach {
                    map[it.nativeID] = it.title
                }

                setItems({ caption: String, text: String -> caption.contains(text, ignoreCase = true) },
                        map.keys)

                setItemCaptionGenerator {
                    map[it]
                }

                isEmptySelectionAllowed = false
                isTextInputAllowed = false

            }
            button("查询") {
                alignment = Alignment.BOTTOM_LEFT
                onLeftClick {
                    val appID = cbApp.value

                    versions.clear()
                    versions.addAll(versionService.queryVersion(appID, null))

                    dataProvider.refreshAll()
                }
            }
        }

        grid(AppVersionRsp::class, "", dataProvider) {
            setSizeFull()
            expandRatio = 1.0f
            showColumns(AppVersionRsp::version,
                    AppVersionRsp::type,
                    AppVersionRsp::createDate,
                    AppVersionRsp::describe,
                    AppVersionRsp::enable)

            addColumn({ if (true == it.enable) "禁用" else "启用" },
                    ButtonRenderer { clickEvent ->
                        val item = clickEvent.item!!
                        versionService.toggleVersion(item.type!!.value, item.appID!!, item.version!!)
                        item.enable = !item.enable!!
                        dataProvider.refreshItem(clickEvent.item)
                    })

//            addColumn({ "QRCode" },
//                    ButtonRenderer { clickEvent ->
//                        val item = clickEvent.item!!
//
//                        val appID = item.appID!!
//                        val appRoot = configService.queryValue(ConfigService.KEYAppRoot)
//                        val appURL = "$appRoot/app/inHouse/$appID?"
//                        //versionService.removeVersion(item.type!!.value, item.appID!!, item.version!!)
//                        //versions.remove(item)
//                        //dataProvider.refreshAll()
//                    })

            addColumn({ "删除" },
                    ButtonRenderer { clickEvent ->
                        val item = clickEvent.item!!
                        versionService.removeVersion(item.type!!.value, item.appID!!, item.version!!)
                        versions.remove(item)
                        dataProvider.refreshAll()
                    })

            expandRatio = 1.0f
            setSizeFull()
        }
    }
}